﻿using System;
using System.Data;
using System.Data.SqlClient;
using DBUtility;
using System.Collections.Generic;
using System.Text;
using IDAL.RF.PIM;
using Model;

namespace DAL.RF.PIM
{
    class PIMTest_temp : BaseRepository_DT<PIMTest_tempInfo>, IPIMTest_temp
    {
        #region 静态变量定义部分
        private const string PARM_TEST_PRODUCTSN = "@ProductSN";
        private const string PARM_TEST_TESTTIME = "@TestTime";
        private const string PARM_TEST_PRODUCTTYPEID = "@ProductTypeID";
        private const string PARM_TEST_USERID = "@UserID";
        private const string PARM_TEST_PCIP = "@PCIP";
        private const string PARM_TEST_SIGNALSOURCE1 = "@SignalSource1";
        private const string PARM_TEST_SIGNALSOURCE2 = "@SignalSource2";
        private const string PARM_TEST_SPECTRUMANALYZER = "@SpectrumAnalyzer";
        private const string PARM_TEST_TESTSTEP = "@TestStep";
        private const string PARM_TEST_ISPASS = "@IsPass";
        private const string PARM_TEST_MORECONTENT = "@MoreContent";
        private const string PARM_TEST_TESTBAND = "@TestBand";
        private const string PARM_TEST_FLAG = "@Flag";

        private const string PARM_TEST_TESTANDITEMDT = "@PIMTestItemdt";
        #endregion

        #region 数据操作部分
        //-------------------------------------用于射频PIM测试数据录入的Web Service-----------------------------------------------------
        /// <summary>
        /// 测试记录与详细数据同时写入
        /// </summary>
        /// <param name="asi">测试记录信息</param>
        /// <param name="itemdt">测试详细数据表</param>
        /// <returns></returns>
        public bool PIMTest_tempAndItem_Insert(PIMTest_tempInfo ti, DataTable itemdt)
        {
            List<SqlParameter> ItemList = new List<SqlParameter>() {
            sqlHelper.CreateSqlParemeterStructured(PARM_TEST_TESTANDITEMDT,itemdt)};
            SqlParameter[] Parms = GetParameters();
            SetParameters(ti, Parms);

            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm);
            foreach (SqlParameter parm in ItemList)
                cmd.Parameters.Add(parm);

            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                try
                {
                    cmd.CommandText = "PIMTestAndItem_temp_Insert";
                    using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { }
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public IList<PIMTest_tempInfo> PIMTest_temp_GetLatestbyProductSN(string productSN)
        {
            SqlParameter parm = new SqlParameter(PARM_TEST_PRODUCTSN, SqlDbType.NVarChar, 50);
            parm.Value = productSN;
            return ExcuteSqlToGetEntities("PIMTest_temp_selectLatestbyProductSN", parm);
        }

        public void PIMTest_temp_DisableRecordsbySN(string productSN)
        {
            SqlParameter parm = new SqlParameter(PARM_TEST_PRODUCTSN, SqlDbType.NVarChar, 50);
            parm.Value = productSN;
            sqlHelper.ExecuteNonQuery(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "PIMTest_temp_UpdateFlagbyProductSN", parm);
        }
        #endregion

        #region 重写基类部分
        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
                    new SqlParameter(PARM_TEST_PRODUCTSN, SqlDbType.NVarChar,50),
					new SqlParameter(PARM_TEST_TESTTIME, SqlDbType.DateTime, 8),
					new SqlParameter(PARM_TEST_PRODUCTTYPEID, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_TEST_USERID ,SqlDbType.NVarChar ,20),
                    new SqlParameter(PARM_TEST_PCIP , SqlDbType.NVarChar, 50),
					new SqlParameter(PARM_TEST_SIGNALSOURCE1 , SqlDbType.NVarChar ,50) ,
                    new SqlParameter(PARM_TEST_SIGNALSOURCE2 , SqlDbType.NVarChar, 50),
					new SqlParameter(PARM_TEST_SPECTRUMANALYZER , SqlDbType.NVarChar ,50) ,
					new SqlParameter(PARM_TEST_TESTSTEP  , SqlDbType.NVarChar ,50) ,
                    new SqlParameter(PARM_TEST_ISPASS  , SqlDbType.NVarChar, 10),
					new SqlParameter(PARM_TEST_MORECONTENT  , SqlDbType.NVarChar ,50),
                    new SqlParameter(PARM_TEST_TESTBAND   , SqlDbType.NVarChar, 20),
					new SqlParameter(PARM_TEST_FLAG   , SqlDbType.Bit ,1) 
                };
            return parms;
        }

        public override void SetParameters(PIMTest_tempInfo Entity, params SqlParameter[] Parms)
        {
            Parms[0].Value = Entity.ProductSN;
            Parms[1].Value = Entity.TestTime;
            Parms[2].Value = Entity.ProductTypeID;
            Parms[3].Value = Entity.UserID;
            Parms[4].Value = Entity.PCIP;
            Parms[5].Value = Entity.SignalSource1;
            Parms[6].Value = Entity.SignalSource2;
            Parms[7].Value = Entity.SpectrumAnalyzer;
            Parms[8].Value = Entity.TestStep;
            Parms[9].Value = Entity.IsPass;
            Parms[10].Value = Entity.MoreContent;
            Parms[11].Value = Entity.TestBand;
            Parms[12].Value = Entity.Flag;
        }
        #endregion

        //-------------------------------------重写超类基础操作---------------------------------------------------------

        public override PIMTest_tempInfo GetEntityRecord(SqlDataReader rdr)
        {
            PIMTest_tempInfo pro = new PIMTest_tempInfo() 
            {
                ProductSN=rdr.GetValue(0).ToString(),
                TestTime=rdr.GetDateTime(1),
                ProductTypeID = rdr.GetValue(2).ToString(),
                UserID = rdr.GetValue(3).ToString(),
                PCIP = rdr.GetValue(4).ToString(),
                SignalSource1 = rdr.GetValue(5).ToString(),
                SignalSource2 = rdr.GetValue(6).ToString(),
                SpectrumAnalyzer = rdr.GetValue(7).ToString(),
                TestStep = rdr.GetValue(8).ToString(),
                IsPass = rdr.GetValue(9).ToString(),
                MoreContent = rdr.GetValue(10).ToString(),
                TestBand = rdr.GetValue(11).ToString()
            };
            return pro;
        }

        public override PIMTest_tempInfo GetEntityRecord()
        {
            return new PIMTest_tempInfo();
        }
    }
}
